package com.cskaoyan;

import com.cskaoyan.dao.UserDao;
import com.cskaoyan.utils.MybatisUtils;
import com.cskaoyan.vo.User;
import com.cskaoyan.vo.UserBO;
import com.cskaoyan.vo.UserVO;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

/**
 * Mybatis 输出映射
 */
public class MybatisTestOutput {

    static SqlSession sqlSession = null;
    static {
        sqlSession = MybatisUtils.getSqlSession();
    }

    /**
     * 简单类型 Integer
     */
    @Test
    public void testSelectCount(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        Integer count = userDao.selectCount();

        System.out.println(count);

    }
    /**
     * 简单类型 String
     */
    @Test
    public void testSelectNameById(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        String name = userDao.selectNameById(1001);

        System.out.println(name);

    }

    /**
     * 多条数据，简单类型
     */
    @Test
    public void testSelectUsernames(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        System.out.println(userDao.selectUserNames());
    }

    /**
     * 多条数据，简单类型
     */
    @Test
    public void testSelectUsernamesToArray(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        String[] strings = userDao.selectUserNamesToArray();

        for (String string : strings) {
            System.out.println(string);
        }
    }

    /**
     * JavaBean
     */
    // 单条数据
    @Test
    public void testSelectOneById(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = userDao.selectById(1001);

        System.out.println(user);

    }

    /**
     * 模拟列名和表名不一致的情况
     */
    @Test
    public void testSelectOneById2(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);
        UserVO user = userDao.selectById2(1001);

        System.out.println(user);

    }

    /**
     * 多条结果
     */
    @Test
    public void testSelectList(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        List<UserVO> userVOS = userDao.selectList();

        userVOS.forEach(userVO -> System.out.println(userVO));

    }

    /**
     * resultMap封装
     */
    @Test
    public void testSelectBO(){

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        List<UserBO> userBOS = userDao.selectListBO();

        userBOS.forEach(userBO ->
                        System.out.println(userBO)
                );

    }








//    @After
//    public void testAfter(){
//
//        System.out.println("sqlSession close...");
//        sqlSession.close();
//    }

    @AfterClass
    public static void testAfterClass(){

        System.out.println("sqlSession close...");
        sqlSession.close();
    }
}
